﻿<?php
	require_once("../class/mysql_help.class.php");
	class Message_Reply
	{
		var $sqlHelper;
		
		/*构造函数*/
		public function __construct()
		{
			$this->sqlHelper = new db_Mysql();
		}
		
		/*功能：添加回复
		@ $id_board  留言记录的id
		@ $type_role  留言人的角色代号(企业或者毕业生)
		@ $id_role  留言人的id(毕业生或者企业的id)
		@ $content  留言内容
		//返回：新增记录的id
		*/
		public function AddReply($id_board,$type_role,$id_role,$content)
		{
			//获取留言人的名字
			$cmd = null;
			if($type_role == 1)//毕业生
			{
				$cmd = "Select name From graduate Where id = '".$id_role."'";
			}
			else if($type_role == 2)//企业
			{
				$cmd = "Select companyName as name From company Where id = '".$id_role."'";
			}
			//查询数据库获取名字
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$row = $this->sqlHelper->fetch_array($result);
			$this->sqlHelper->free_result($result);
			$this->sqlHelper->dbclose();
			
			//将数据插入到
			$date_public = time();
			$cmd = "Insert Into message_reply (type_role,id_role,content,date_public,id_board,name) values('".
				$type_role."','".$id_role."','".$content."','".$date_public."','".$id_board."','".$row["name"]."')";
			$this->sqlHelper->dbconnect();
			$id_added = $this->sqlHelper->insert($cmd);//获取新增记录的id
			$this->sqlHelper->dbclose();
			
			//使对应的评论的回复加1
			include("../class/messageBoard.class.php");
			$class_msgBoard = new MessageBoard();
			$class_msgBoard->AddReplyNum($id_board);
			return $id_added;//返回新增记录的id
		}
		
		/*功能：删除回复
		@ $id_reply  回复的id
		//返回：受影响的（被删除的）记录数，成功为1，否则为0
		*/
		public function DeleteReplyById($id_reply)
		{
			$cmd = "Delete * from message_reply where id = '".$id_reply."'";
			$this->sqlHelper->dbconnect();
			$num_delete = $this->sqlHelper->delete($cmd);//获取被删除的记录数
			$this->sqlHelpr->dbclose();
			return $num_delete;//返回被删除的记录数
		}
			
		/*修改（暂时不需要）*/
		
		/*功能：获取某条留言的所有回复数
		@ $id_messageBoard  留言的id
		//返回：搜索出的所有记录存放在$result里面。
		//注意：用完返回的$result记得free哦亲！
		*/
		public function SelectAllReplyByMessgeBoardId($id_messageBoard)
		{
			$cmd = "Select * From message_reply where id_board = '".$id_messageBoard."' order by date_public desc";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);//获取搜索出的记录
			$this->sqlHelper->dbclose();
			return $result;//返回记录数
		}
		
		/*功能：搜索一条回复
		@ $id_reply 对应回复的id
		//返回，数组化的记录$row
		*/
		public function SelectOneReplyById($id_reply)
		{
			$cmd = "Select * From message_reply Where id = '".$id_reply."'";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			$row = $this->sqlHelper->fetch_array($result);
			$this->sqlHelper->free_result($result);
			return $row;
		}
		
		/*功能：展示一条回复
		@ $id_reply 需要展示的回复的id
		//返回：展示回复的html类型的$str
		*/
		public function ShowOneReplyById($id_reply)
		{
			$row = $this->SelectOneReplyById($id_reply);
			$str ='<div class="message_sep"></div>
					<div class="message_q">
						<div class="message_q_logo"><img src="../data/company_logo/13266194330001519mm.png" width="40" height="40"  /></div>
						<div class="message_q_message"><span class="fc_cpn_names">'.$row["name"].'：</span>'.$row["content"].'</div>
						<div class="clear"></div>
						<div class="message_re_q">'.date("Y-m-d",$row["date_public"]).'</div>
					</div>';
			return $str;
		}
		
		/*功能：展示所有的回复
		@ $id_messageBoard 对应留言的id
		*/
		public function ShowAllReplyByCommentId($id_messageBoard)
		{
			$result = $this->SelectAllReplyByMessgeBoardId($id_messageBoard);
			$str = "";
			while($row = $this->sqlHelper->fetch_array($result))
			{
				$str .= '
					<div class="message_sep"></div>
					<div class="message_q">
						<div class="message_q_logo"><img src="../data/company_logo/13266194330001519mm.png" width="40" height="40"  /></div>
						<div class="message_q_message"><span class="fc_cpn_names">'.$row["name"].'：</span>'.$row["content"].'</div>
						<div class="clear"></div>
						<div class="message_re_q">'.date("Y-m-d",$row["date_public"]).'</div>
					</div>';
			}
			return $str;
		}
		
	}
		
?>
		